﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell;
using System.Management.Automation;
using AutocompleteDemo;

namespace AutocompleteDemo.Cmdlet
{
    public sealed class AutoCompletionServiceApplicationPipeBind : SPCmdletPipeBind<AutoCompletionServiceApplication>
    {
        // Fields
        private Guid m_applicationId;
        private string m_applicationName;
        internal const string SomeProperty = "SomeProperty";

        // Methods
        public AutoCompletionServiceApplicationPipeBind(AutoCompletionServiceApplication applicationObject)
            : base(applicationObject)
        {
            this.m_applicationId = Guid.Empty;
        }

        public AutoCompletionServiceApplicationPipeBind(Guid guid)
        {
            this.m_applicationId = Guid.Empty;
            this.m_applicationId = guid;
        }

        public AutoCompletionServiceApplicationPipeBind(string name)
        {
            this.m_applicationId = Guid.Empty;
            this.m_applicationName = name;
        }

        protected override void Discover(AutoCompletionServiceApplication instance)
        {
            this.m_applicationName = instance.Name;
            this.m_applicationId = instance.Id;
        }

        public override AutoCompletionServiceApplication Read()
        {
            AutoCompletionServiceApplication webApplicationByName = null;
            if (Guid.Empty == this.m_applicationId)
            {
                if (!string.IsNullOrEmpty(this.m_applicationName))
                {
                    webApplicationByName = AutoCompletionService.Local.GetWebApplicationByName(this.m_applicationName);
                }
                if (webApplicationByName == null)
                {
                    try
                    {
                        Guid applicationId = new Guid(this.m_applicationName);
                        webApplicationByName = AutoCompletionService.Local.GetWebApplicationById(applicationId);
                    }
                    catch (FormatException)
                    {
                    }
                }
            }

            if (webApplicationByName == null)
            {
                if (Guid.Empty == this.m_applicationId)
                {
                    webApplicationByName = AutoCompletionService.Local.GetWebApplicationByName(this.m_applicationName);
                }
                else
                {
                    webApplicationByName = AutoCompletionService.Local.GetWebApplicationById(this.m_applicationId);
                }
            }

            if (webApplicationByName == null)
            {
                throw new ApplicationException(string.Format("SettingApplicationNameNotFound", new object[] { this.m_applicationName }));
            }

            if (webApplicationByName != null)
            {
                webApplicationByName.DemandAdministrationReadAccess();
            }

            return webApplicationByName;
        }
    }


}
